#!/bin/bash

# Describe:     download mac packages
# Create Date： 2021-10-26
# Create Time:  20:36
# Update Date:  2021-10-29
# Update Time:  13:37
# Author:       MiaoCunFa
# Version:      v0.0.10
#
# Usage:        /bin/zsh -c "$(curl -fsSL https://gitee.com/miaocunfa/mac/raw/master/mac_down.sh)"
#

#===================================================================

download=$HOME/Downloads
gitVersion="git-2.33.0-intel-universal-mavericks.dmg"
itermVersion="iTerm2-3_4_12.zip"
ohmyzsh="ohmyzsh-miao.tar"

# jetbrains
javaPro="ideaU-2021.2.3-aarch64.dmg"
javaComm="ideaC-2021.2.3-aarch64.dmg"
pythonPro="pycharm-professional-2021.2.3-aarch64.dmg"
pythonComm="pycharm-community-2021.2.3-aarch64.dmg"
Goland="goland-2021.2.4-aarch64.dmg"
DataGrip="datagrip-2021.2.4-aarch64.dmg"

# vpn
clash="ClashX-1.70.0.dmg"
westworld="西部世界VPN-1.16.dmg"
vpn="Tunnelblick_3.8.6a_build_5711.dmg"

# tool
host="SwitchHosts_arm64_4.0.3.6070.dmg"
code="VSCode-darwin-arm64.zip"
pd="ParallelsDesktop-17.0.1-51482.dmg"
dash="Dash.zip"
oss="oss-browser-darwin-x64.zip"

#-------------------------------------------------------------------

exit_handler()
{
    echo
    echo -e "\tQuit the Program!"
    echo
    exit $EXITCODE
    
} # End of exit_handler function


function get_answer()
{
    unset ANSWER
    ASK_COUNT=0

    while [ -z "$ANSWER" ] # While no answer is given, keep asking.
    do 
        echo
        
        ASK_COUNT=$[ $ASK_COUNT + 1 ]
        
        case $ASK_COUNT in 
        2)  
            echo -e "\t请回答这个问题！"
        ;;
         
        3)  
            echo -e "\t再次尝试！请回答这个问题！"
        ;;
         
        4)  
            echo -e "\t因为你拒绝回答这个问题...."
            echo -e "\t程序退出！"
            exit_handler                     # the function Quit Script
        ;;
        esac
      
        if [ -n "$LINE2" ]
        then 
            echo -e "\t"$LINE1
            echo -e "\t"$LINE2" \c"
        else
            echo -e "\t"$LINE1" \c"
        fi
      
        # allow 30 seconds to answer before time-out 
        read -t 30 ANSWER
    done 
    
    # Do a little variable clean-up 
    unset LINE1
    unset LINE2

} # End of get_answer function


function process_answer()
{
    case $ANSWER in
    
    y|Y|YES|yes|Yes|yEs|yeS|YEs|yES)          # If user answer "yes", do nothing.
        echo
        echo -e "\t确认操作, 继续处理!"
    ;;
    
    *)                                        # If user answer anything but "yes", exit script.
        echo
        echo -e "\t"$EXIT_LINE1
        echo -e "\t"$EXIT_LINE2
        exit_handler                          # the function Quit Script
    ;;
      
    esac
    
    # Do a little variable clean-up 
    unset EXIT_LINE1
    unset EXIT_LINE2
    
} # End of process_answer function

function download_answer(){

    case $ANSWER in
        y|Y|YES|yes|Yes|yEs|yeS|YEs|yES)          # If user answer "yes", download.
            if [ ! -f "$download/$OUTPUT" ]
            then
                echo -e "\t"$INFO_LINE1
                curl -o $download/$OUTPUT -O http://gjr-soft-repo.oss-cn-qingdao.aliyuncs.com/mac/$REMOTE
            fi
        ;;
    
        *)                                        # If user answer "no", do nothing                       
        ;;
    esac

    unset OUTPUT
    unset REMOTE
}


#-------------------------------------------------------------------

echo
echo -e "\t本脚本先在 终端运行"
echo -e "\t用户目录为: $HOME"
echo -e "\t下载目录为: $download"
echo -e "\t脚本开始下载后, 请暂时不要清理下载文件!!!"
echo

if [ ! -f "$download/$gitVersion" ]
then
    echo -e "\t开始下载: git"
    curl -o $download/$gitVersion -O http://gjr-soft-repo.oss-cn-qingdao.aliyuncs.com/mac/$gitVersion
fi

if [ ! -f "$download/$itermVersion" ]
then
    echo
    echo -e "\t开始下载: iterm2"
    curl -o $download/$itermVersion -O http://gjr-soft-repo.oss-cn-qingdao.aliyuncs.com/mac/$itermVersion
fi

if [ ! -f "$download/$ohmyzsh" ]
then
    echo
    echo -e "\t开始下载: ohmyzsh主题"
    curl -o $download/$ohmyzsh -O http://gjr-soft-repo.oss-cn-qingdao.aliyuncs.com/mac/$ohmyzsh
fi

echo
echo -e "\t普通环境已下载完成"
echo -e "\t请打开下载目录$download, 在桌面右下角位置"
echo -e "\t手动安装刚才下好的git和iTerm"
echo -e "\t安装好iTerm后, 使用iTerm继续执行接下来的操作"
echo

#-------------------------------------------------------------------

LINE1="是否继续下载开发者环境[y/n]: "
get_answer

EXIT_LINE1="因为你不希望安装开发者环境！"
EXIT_LINE2="所以, 退出这个脚本！"
process_answer

LINE1="是否下载jetbrains公司的 Java企业版[y/n]: "
get_answer

INFO_LINE1="开始下载jetbrains公司的 Java企业版"
OUTPUT=$javaPro
REMOTE=$javaPro
download_answer

#-------------------------------------------------------------------

LINE1="是否下载jetbrains公司的 Java社区版[y/n]: "
get_answer

INFO_LINE1="开始下载jetbrains公司的 Java社区版"
OUTPUT=$javaComm
REMOTE=$javaComm
download_answer

#-------------------------------------------------------------------

LINE1="是否下载jetbrains公司的 Python企业版[y/n]: "
get_answer

INFO_LINE1="开始下载jetbrains公司的 Python企业版"
OUTPUT=$pythonPro
REMOTE=$pythonPro
download_answer

#-------------------------------------------------------------------

LINE1="是否下载jetbrains公司的 Python社区版[y/n]: "
get_answer

INFO_LINE1="开始下载jetbrains公司的 Python社区版"
OUTPUT=$pythonComm
REMOTE=$pythonComm
download_answer

#-------------------------------------------------------------------

LINE1="是否下载jetbrains公司的 Goland[y/n]: "
get_answer

INFO_LINE1="开始下载jetbrains公司的 Goland"
OUTPUT=$Goland
REMOTE=$Goland
download_answer

#-------------------------------------------------------------------

LINE1="是否下载jetbrains公司的 DataGrip[y/n]: "
get_answer

INFO_LINE1="开始下载jetbrains公司的 DataGrip"
OUTPUT=$DataGrip
REMOTE=$DataGrip
download_answer

#-------------------------------------------------------------------

LINE1="是否下载 $clash [y/n]: "
get_answer

INFO_LINE1="开始下载 $clash"
OUTPUT=$clash
REMOTE=$clash
download_answer

#-------------------------------------------------------------------

LINE1="是否下载 $westworld [y/n]: "
get_answer

INFO_LINE1="开始下载 $westworld"
OUTPUT=$westworld
REMOTE=$westworld
download_answer

#-------------------------------------------------------------------

LINE1="是否下载 $vpn [y/n]: "
get_answer

INFO_LINE1="开始下载 $vpn"
OUTPUT=$vpn
REMOTE=$vpn
download_answer

#-------------------------------------------------------------------

LINE1="是否下载 $code [y/n]: "
get_answer

INFO_LINE1="开始下载 $code"
OUTPUT=$code
REMOTE=$code
download_answer

#-------------------------------------------------------------------

LINE1="是否下载 $host [y/n]: "
get_answer

INFO_LINE1="开始下载 $host"
OUTPUT=$host
REMOTE=$host
download_answer

#-------------------------------------------------------------------

LINE1="是否下载 $pd [y/n]: "
get_answer

INFO_LINE1="开始下载 $pd"
OUTPUT=$pd
REMOTE=$pd
download_answer

#-------------------------------------------------------------------

LINE1="是否下载 $dash [y/n]: "
get_answer

INFO_LINE1="开始下载 $dash"
OUTPUT=$dash
REMOTE=$dash
download_answer

#-------------------------------------------------------------------

LINE1="是否下载 $oss [y/n]: "
get_answer

INFO_LINE1="开始下载 $oss"
OUTPUT=$oss
REMOTE=$oss
download_answer
